window.silder = null;
var silderPause=false;
$(function () {
   obj.init();
});
var obj =new function () {
  'use strict';
  return {
        targetFrame:null,
        noticeList: null,
        title     : null,
        banner    : null,
        init: function () {
            this.targetFrame = $('#targetFrame',window.parent.document);
            this.noticeList  = $('#noticeList');
            this.title       = $('#targetPanel-title',window.parent.document);
            this.banner      = $('.flexslider');

            try{
                window.top.chageColor('#134c79');
                // window.webkit.messageHandlers.setBarColor.postMessage({color:'#134c79'});
            }catch (Expection){

            }
            this.funObj().initBanner();
            this.funObj().initData();
            this.listener();
        },
        listener: function () {
            var $this = this;
            //通知公告点击事件
            $this.noticeList.on('click','.clickNotice',function () {
                var id=$(this).attr('id');
                $this.targetFrame.attr('src',baseUrl+'mobile/home/noticeDetail?iNoId='+id);
                $this.title.html('');
                window.parent.goTarget();
            });

            //通知公告更多点击事件
            $this.noticeList.on('click','.more',function () {
                $this.targetFrame.attr('src',baseUrl+'mobile/home/moreNotice');
                $this.title.html('更多');
                window.parent.goTarget();
            });

            //信息点击事件
            $this.noticeList.on('click','.clickMsg',function () {
                var id=$(this).attr('id');
                $this.targetFrame.attr('src',baseUrl+'mobile/home/msgDetail?iArtId='+id);
                $this.title.html('');
                window.parent.goTarget();
            });

            //信息更多点击事件
            $this.noticeList.on('click','.moreMsg',function () {
                var id=$(this).attr('id');
                var type=$(this).attr('tag');
                $this.targetFrame.attr('src',baseUrl+'mobile/home/moreMsg?iArtType='+type);
                $this.title.html('更多');
                window.parent.goTarget();
            });

            //公开课点击时间
            $this.noticeList.on('click','.clickClass',function () {
                var id=$(this).attr('id');
                $this.targetFrame.attr('src',baseUrl+'mobile/home/classDetail?iPCId='+id);
                $this.title.html('');
                window.parent.goTarget();
            });

            //公开课更多点击事件
            $this.noticeList.on('click','.moreClass',function () {
                var id=$(this).attr('id');
                $this.targetFrame.attr('src',baseUrl+'mobile/home/moreClass');
                $this.title.html('');
                window.parent.goTarget();
            });
        },
        funObj:function () {
            var $this = this;
            return {
                initData:function () {
                    $.ajax({
                        url:baseUrl+'mobile/home/init',
                        type:'POST',
                        dataType:'json',
                        success:function (data) {
                            console.log(data);
                            var str ='';
                            str += $this.funObj().fillNotice(data['notice']);
                            str += $this.funObj().fillMsg(data['appMsg']);
                            str += $this.funObj().fillMsg(data['tchMsg']);
                            str += $this.funObj().fillMsg(data['planMsg']);
                            str += $this.funObj().fillPClass(data['pClass']);
                            $this.noticeList.empty().append(str);

                        },
                        error:function (data) {
                            console.log(data);
                        }
                    });
                },
                fillNotice: function(notice){
                    var content='';
                    if (notice['power']) {
                        if(notice['data'].length == 0){
                            return '';
                        }
                        $.each(notice['data'],function (id,item) {
                            content+=' <li>'+
                                '<div >'+
                                '<div><span class="type">'+item['iNoType']+'</span><span><a id="'+item['iNoId']+'" class="'+notice['class']+' " tag="'+notice['tag']+'"  >'+item['sTitle']+'</a></span></div>'+
                                '<div style="margin-bottom: 10px"><span >'+item['tTime']+'</span><span style="margin-left: 10px"><i class="fa fa-eye" aria-hidden="true" style="margin-right: 5px"></i>'+item['count']+'</span><span style="float: right"><a class="more">更多</a></span></div>'+
                                '</div>'+
                                '</li>';
                        });
                    }
                    return content;
                },
                fillMsg: function (msg) {
                    var content = '';
                    if (msg['power']) {
                        if (msg['data'].length == 0) {
                            return '';
                        }
                        $.each(msg['data'], function (id, item) {
                            content += ' <li>' +
                                '<div >' +
                                '<div><span class="type">' + item['iArtType'] + '</span><span><a id="' + item['iArtId'] + '" class="' + msg['class'] + '" tag="' + msg['tag'] + '">' + item['sTitle'] + '</a></span></div>' +
                                '<div style="margin-bottom: 10px"><span >' + item['tStartTime'] + '</span><span style="float: right"><a class="moreMsg" tag="' + msg['tag'] + '" >更多</a></span></div>' +
                                '</div>' +
                                '</li>';
                        });
                    }
                    return content;
                },
                fillPClass: function (msg) {
                    var content = '';
                    if (msg['power']) {
                        if (msg['data'].length == 0) {
                            return '';
                        }
                        $.each(msg['data'], function (id, item) {
                            content += ' <li>' +
                                '<div >' +
                                '<div><span class="type">' + item['iPCType'] + '</span><span><a id="' + item['iPCId'] + '" class="' + msg['class'] + '" tag="' + msg['tag'] + '">' + item['sSchoolName'] + '    ' + item['sUserName'] + '</a></span></div>' +
                                '<div style="margin-bottom: 10px"><span >' + item['tStartTime'] + '</span><span style="float: right"><a class="moreClass" tag="' + msg['tag'] + '" >更多</a></span></div>' +
                                '</div>' +
                                '</li>';
                        });
                    }
                    return content;
                },
                initBanner:function(){
                    $.ajax({
                        url:baseUrl+'mobile/index/getBanner',
                        dataType:'json',
                        type:'post',
                        success:function (data) {
                            $this.banner.empty();
                            var str = '<ul class="slides">';
                            $.each(data,function (i,item) {
                                str += '<li><img src="'+item['img']+'" targetUrl="'+item['url']+'"  stusColor="'+item['color']+'" style="height:150px;"></li>';
                            });
                            str +='</ul>';
                            $this.banner.append(str);
                            silder = $this.banner.flexslider({
                                animation: 'slide',
                                animationLoop: true, //是否循环播放
                                controlNav: false,
                                directionNav: false,
                                startAt: 0, // Integer: 开始播放的 slide，从 0 开始计数
                                slideshow: true, // Boolean: 是否自动播放
                                slideshowSpeed: 3000, // Integer: ms 滚动间隔时间
                                animationSpeed: 600, // Integer: ms 动画滚动速度
                                animationDuration: 600,
                                direction: "vertical",
                                // pauseInvisible: ,
                                // pausePlay:true,
                                touch:false,
                                pauseOnAction:true,
                                before: function(silder){
                                    var color = $this.banner.find('.flex-active-slide').next().find('img').attr('stuscolor');
                                    try{
                                        window.top.chageColor(color);
                                    }catch (e){

                                    }

                                }
                            });
                        },
                        error: function (res) {
                            window.top.sendErrorMsg(res['error']);
                        }
                    });
                },
                startSilder:function(){
                   silder.data('flexslider').play();
                }
            };
        }
  };
};